package com.foreknow.elm.service.Impl;

import com.foreknow.elm.dao.DeliveryaddressDao;
import com.foreknow.elm.dao.Impl.DeliveryaddressDaoImpl;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    @Override
    public List<Deliveryaddress> listDeliveryAddressByUserId(String userId) {
        List<Deliveryaddress> list = new ArrayList<>();
        DeliveryaddressDao deliveryaddressDao= new DeliveryaddressDaoImpl();
        try {
            list = deliveryaddressDao.listDeliveryAddressByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public Deliveryaddress getDeliveryAddressById(Integer daId) {
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        DeliveryaddressDao deliveryaddressDao= new DeliveryaddressDaoImpl();
        try {
            deliveryaddress = deliveryaddressDao.getDeliveryAddressById(daId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return deliveryaddress;
    }

    @Override
    public Integer saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId) {
        DeliveryaddressDao deliveryaddressDao= new DeliveryaddressDaoImpl();
        Integer result = 0;
        try {
            DBUtil.beginTransaction();//开启一个事务
            result = deliveryaddressDao.saveDeliveryAddress(contactName,contactSex,contactTel,address,userId);
            DBUtil.commitTransaction();//提交一个事务

        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();  //回滚一个事务
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }
    @Override
    public Integer updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId) {
        DeliveryaddressDao deliveryaddressDao= new DeliveryaddressDaoImpl();
        Integer result = 0;
        try {
            DBUtil.beginTransaction();//开启一个事务
            result = deliveryaddressDao.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address,userId);
            DBUtil.commitTransaction();//提交一个事务

        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();  //回滚一个事务
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }

    @Override
    public Integer removeDeliveryAddress(Integer daId) {
        DeliveryaddressDao deliveryaddressDao= new DeliveryaddressDaoImpl();
        Integer result = 0;
        try {
            DBUtil.beginTransaction();//开启一个事务
            result = deliveryaddressDao.removeDeliveryAddress(daId);
            DBUtil.commitTransaction();//提交一个事务

        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();  //回滚一个事务
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }
}